﻿#ifndef __SS_ATOMIC_LOCK_H__
#define __SS_ATOMIC_LOCK_H__
#include "ssAtomic.h"

namespace StarSeeker
{

class ssAtomicLock
{
public:
	ssAtomicLock(void)
		:m_reference(0)
	{
	}

	inline void Lock()
	{
		unsigned int n=0;
		while (1 == m_reference.CompareExchange(0, 1))
		{
			if (n < 16)
			{
			}
			else if (n < 64)
			{
#ifdef SS_USE_WINDOWS
				::Sleep(0);
#else
				::usleep(0);
#endif
			}
			else
			{
#ifdef SS_USE_WINDOWS
				::Sleep(1);
#else
				::usleep(1000);
#endif
			}
			++n;
		}
	}

	inline void Unlock()
	{
		m_reference = 0;
	}

private:
	ssAtomic<long>	m_reference;
};


}
#endif
